﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BattleManager {
	//战场中的坦克
	public static Dictionary<string, BaseTank> tanks = new Dictionary<string, BaseTank>();

	//初始化
	public static void Init()
	{
		Debug.LogWarning("Init");
		NetManager.AddMsgListener("MsgEnterBattle", OnMsgEnterBattle);
		NetManager.AddMsgListener("MsgBattleResult", OnMsgBattleResult);
		NetManager.AddMsgListener("MsgLeaveBattle", OnMsgLeaveBattle);
		
		NetManager.AddMsgListener("MsgSyncTank", OnMsgSyncTank);
		NetManager.AddMsgListener("MsgFire", OnMsgFire);
		NetManager.AddMsgListener("MsgHit", OnMsgHit);
	}

	//添加坦克
	public static void AddTank(string id, BaseTank tank){
		tanks[id] = tank;
	}

	//删除坦克
	public static void RemoveTank(string id){
		tanks.Remove(id);
	}

	//获取坦克
	public static BaseTank GetTank(string id) {
		if(tanks.ContainsKey(id)){
			return tanks[id];
		}
		return null;
	}

	//获取玩家控制的坦克
	public static BaseTank GetCtrlTank() {
		return GetTank(GameMain.id);
	}
	
	//重置战场
	public static void Reset() {
		//场景
		foreach(BaseTank tank in tanks.Values){
			MonoBehaviour.Destroy(tank.gameObject);
		}
		//列表
		tanks.Clear();
	}
	
	public static void OnMsgEnterBattle(MsgBase msgBase){
		Debug.Log("123");
		MsgEnterBattle msg = (MsgEnterBattle)msgBase;
		EnterBattle(msg);
	}
	
	public static void EnterBattle(MsgEnterBattle msg) {
		//重置
		BattleManager.Reset();
		//关闭界面
		PanelManager.Close("RoomPanel");//可以放到房间系统的监听中
		PanelManager.Close("ResultPanel");
		//产生坦克
		for(int i=0; i<msg.tanks.Length; i++){
			GenerateTank(msg.tanks[i]);
		}
	}
	
	public static void GenerateTank(TankInfo tankInfo){
		//GameObject
		string objName = "Tank_" + tankInfo.id;
		GameObject tankObj = new GameObject(objName);
		//AddComponent
		BaseTank tank = null;
		if(tankInfo.id == GameMain.id) {
			tank = tankObj.AddComponent<CrtlTank>();
		}
		else {
			tank = tankObj.AddComponent<SyncTank>();
		}
		//camera
		if(tankInfo.id == GameMain.id) {
			CameraFollow cf = tankObj.AddComponent<CameraFollow>(); 
		}
		//属性
		tank.camp = tankInfo.camp;
		tank.id = tankInfo.id;
		//init
		if(tankInfo.camp == 1){
			tank.Init("tankPrefab1");
		}
		else{
			tank.Init("tankPrefab2");
		}
		//pos rotation
		Vector3 pos = new Vector3(tankInfo.x, tankInfo.y, tankInfo.z);
		Vector3 rot = new Vector3(tankInfo.ex, tankInfo.ey, tankInfo.ez);
		tank.transform.position = pos;
		tank.transform.eulerAngles = rot;
		//列表
		AddTank(tankInfo.id, tank);
	}
	
	
	public static void OnMsgBattleResult(MsgBase msgBase){
		MsgBattleResult msg = (MsgBattleResult)msgBase;
		//判断显示胜利还是失败
		bool isWin = false;
		BaseTank tank = GetCtrlTank();
		if(tank!= null && tank.camp == msg.winCamp){
			isWin = true;
		}
		//显示界面
		PanelManager.Open<ResultPanel>(isWin);
	}
	
	public static void OnMsgLeaveBattle(MsgBase msgBase){
		MsgLeaveBattle msg = (MsgLeaveBattle)msgBase;
		//查找坦克
		BaseTank tank = GetTank(msg.id);
		if(tank == null){
			return;
		}
		//删除坦克
		RemoveTank(msg.id);
		MonoBehaviour.Destroy(tank.gameObject);
	}

	public static void OnMsgSyncTank(MsgBase msgBase)
	{
		MsgSyncTank msg = (MsgSyncTank)msgBase;

		if (msg.id==GameMain.id)
		{
			return;
		}

		SyncTank tank = (SyncTank)GetTank(msg.id);
		tank.SyncPos(msg);
	}
	
	public static void OnMsgFire(MsgBase msgBase)
	{
		MsgFire msg = (MsgFire)msgBase;

		if(msg.id == GameMain.id){
			return;
		}

		SyncTank tank = (SyncTank)GetTank(msg.id);
		if(tank == null){
			return;
		}

		tank.SyncFire(msg);
	}
	
	public static void OnMsgHit(MsgBase msgBase){
		MsgHit msg = (MsgHit)msgBase;

		BaseTank tank = GetTank(msg.targetId);
		if(tank == null){
			return;
		}

		tank.Attacked(msg.damage);
	}
}

